package interfazGrafica;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;

import negocios.Minibook;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EliminarUsuario extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField txMail;
	private Minibook book;
	private Pattern pat = Pattern.compile("^[\\w-]+(\\.[\\w-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			EliminarUsuario dialog = new EliminarUsuario();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public EliminarUsuario() {
		setTitle("Borrar Usuario");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblEliminarUsuario = new JLabel("Eliminar Usuario");
		lblEliminarUsuario.setFont(new Font("Dialog", Font.BOLD, 20));
		lblEliminarUsuario.setForeground(Color.BLUE);
		lblEliminarUsuario.setBounds(109, 12, 205, 26);
		contentPanel.add(lblEliminarUsuario);
		
		JLabel lblIngreseMail = new JLabel("Ingrese el Email del usuario que desea eliminar:");
		lblIngreseMail.setBounds(12, 63, 394, 15);
		contentPanel.add(lblIngreseMail);
		
		txMail = new JTextField();
		txMail.setBounds(63, 96, 312, 19);
		contentPanel.add(txMail);
		txMail.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Mail:");
		lblNewLabel.setBounds(12, 98, 70, 15);
		contentPanel.add(lblNewLabel);
		
		JButton btnEliminar = new JButton("Eliminar");
		btnEliminar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String mail= txMail.getText();
				Matcher mat = pat.matcher(mail);
				
				book = Principal.getMinibook();
				if (mat.find()){
					if (book.estaRegistrado(mail)){
						
						book.eliminarUsuario(mail);
						
						JOptionPane.showMessageDialog(contentPanel, "El usuario fue eliminado correctamente");
						Principal.setCantidadUsuarios();
						Principal.refresh();
						setVisible(false);
					}
					else{
						JOptionPane.showMessageDialog(contentPanel, "ERROR: El mail ingresado no esta registrado");
						setVisible(false);
					}
				}else {
					JOptionPane.showMessageDialog(contentPanel, "Ingrese un email valido");
				}
				Principal.refresh();
				
			}
		});
		btnEliminar.setBounds(50, 236, 160, 25);
		contentPanel.add(btnEliminar);
		
		JButton btnSalir = new JButton("Salir");
		btnSalir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				setVisible(false);
				Principal.hacerVisible(true);
			}
		});
		btnSalir.setBounds(222, 236, 160, 25);
		contentPanel.add(btnSalir);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		}
	}
}
